package lleida.festamajor.controllers;

import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import lleida.festamajor.model.Category;
import lleida.festamajor.model.Date;
import lleida.festamajor.model.Event;
import lleida.festamajor.model.Place;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLReader {
	
	public static ArrayList<Event> readEvents(InputStream input) {
	     SAXParserFactory factory = SAXParserFactory.newInstance();
	     SAXParser saxParser = null;
	     
	     final ArrayList<Event> events = new ArrayList<Event>();
	 
	     DefaultHandler handler = new DefaultHandler() {
	 
	    	 protected String name;
	    	 protected int place;
	    	 protected int date;
	    	 protected String time;
	    	 protected String description;
	    	 protected int category;
	    	 protected boolean free;
	    	 
		     private StringBuffer tempValue = new StringBuffer();
		 
		     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		    	 tempValue.setLength(0);
		    	 if (localName.equalsIgnoreCase("event")) {
		    		 name = attributes.getValue("name");
		    		 place = Integer.parseInt(attributes.getValue("place"));
		    		 date = Integer.parseInt(attributes.getValue("date"));
		    		 category = Integer.parseInt(attributes.getValue("category"));
		    		 time = attributes.getValue("time");
		    		 free = Boolean.parseBoolean(attributes.getValue("free"));
		    	 }
		     }
		 
		     public void endElement(String uri, String localName, String qName) throws SAXException {
		    	 if (localName.equalsIgnoreCase("event")) {
		    		 description = tempValue.toString();
		    		 events.add(new Event(name, EventController.getPlace(place), EventController.getDate(date), time, description, EventController.getCategory(category), free));
		    	 }
		     }
		 
		     public void characters(char ch[], int start, int length) throws SAXException {
		    	 tempValue.append(ch, start, length);
		     }
	 
	     };
	     
	     try {
	    	 saxParser = factory.newSAXParser();
	    	 saxParser.parse(input, handler);
	     } catch (Exception e) {
	    	 e.printStackTrace();
	     }
	     return events;
	}
	
	public static ArrayList<Date> readDates(InputStream input) {
	     SAXParserFactory factory = SAXParserFactory.newInstance();
	     SAXParser saxParser = null;
	     
	     final ArrayList<Date> dates = new ArrayList<Date>();
	 
	     DefaultHandler handler = new DefaultHandler() {
	    	 
		     private StringBuffer tempValue = new StringBuffer();
		     private String formated;
		 
		     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		    	 tempValue.setLength(0);
		    	 if (localName.equalsIgnoreCase("date"))
		    		 formated = attributes.getValue("formated");
		     }
		 
		     public void endElement(String uri, String localName, String qName) throws SAXException {
		    	 if (localName.equalsIgnoreCase("date"))
		    		 dates.add(new Date(tempValue.toString(), formated));
		     }
		 
		     public void characters(char ch[], int start, int length) throws SAXException {
		    	 tempValue.append(ch, start, length);
		     }
	 
	     };
	     
	     try {
	    	 saxParser = factory.newSAXParser();
	    	 saxParser.parse(input, handler);
	     } catch (Exception e) {
	    	 e.printStackTrace();
	     }
	     return dates;
	}
	
	public static ArrayList<Place> readPlaces(InputStream input) {
	     SAXParserFactory factory = SAXParserFactory.newInstance();
	     SAXParser saxParser = null;
	     
	     final ArrayList<Place> places = new ArrayList<Place>();
	 
	     DefaultHandler handler = new DefaultHandler() {
	    	 
		     private StringBuffer tempValue = new StringBuffer();
		     private double lat;
		     private double lon;
		 
		     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		    	 tempValue.setLength(0);
		    	 if (localName.equalsIgnoreCase("place")) {
		    		 lat = Double.parseDouble(attributes.getValue("lat"));
		    		 lon = Double.parseDouble(attributes.getValue("lon"));
		    	 }
		     }
		 
		     public void endElement(String uri, String localName, String qName) throws SAXException {
		    	 if (localName.equalsIgnoreCase("place"))
		    		 places.add(new Place(tempValue.toString(), lat, lon));
		     }
		 
		     public void characters(char ch[], int start, int length) throws SAXException {
		    	 tempValue.append(ch, start, length);
		     }
	 
	     };
	     
	     try {
	    	 saxParser = factory.newSAXParser();
	    	 saxParser.parse(input, handler);
	     } catch (Exception e) {
	    	 e.printStackTrace();
	     }
	     return places;
	}
	
	public static ArrayList<Category> readCategories(InputStream input) {
	     SAXParserFactory factory = SAXParserFactory.newInstance();
	     SAXParser saxParser = null;
	     
	     final ArrayList<Category> categories = new ArrayList<Category>();
	 
	     DefaultHandler handler = new DefaultHandler() {
	    	 
		     private StringBuffer tempValue = new StringBuffer();
		     private int count = 0;
		 
		     public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		    	 tempValue.setLength(0);
		     }
		 
		     public void endElement(String uri, String localName, String qName) throws SAXException {
		    	 if (localName.equalsIgnoreCase("category")) {
		    		 categories.add(new Category(count, tempValue.toString()));
		    		 count++;
		    	 }
		     }
		 
		     public void characters(char ch[], int start, int length) throws SAXException {
		    	 tempValue.append(ch, start, length);
		     }
	 
	     };
	     
	     try {
	    	 saxParser = factory.newSAXParser();
	    	 saxParser.parse(input, handler);
	     } catch (Exception e) {
	    	 e.printStackTrace();
	     }
	     return categories;
	}
}
